package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.mapper.TStudentMapper;
import com.esyoga.yoyoga.pojo.TGym;
import com.esyoga.yoyoga.pojo.TStudent;
import com.esyoga.yoyoga.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;


@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Resource
    TStudentMapper tStudentMapper;

    /**
     * 增加或减少学员余额
     * @param money:增加或减少的金额。正数为增加，负数为减少
     * @return
     */
    @Override
    public String updateStudentBalance(BigDecimal money, Integer sId) {
        //获取学员当前余额
        BigDecimal stuBalance = tStudentMapper.selectByPrimaryKey(sId).getsBalance();
        //更新的金额
        BigDecimal stuUpdateBalance = stuBalance.add(money);
        //余额不足
        if(stuUpdateBalance.compareTo(new BigDecimal(0))<0){
            return "余额不足";
        }
        //更新
        TStudent tStudent = new TStudent();
        tStudent.setsId(sId);
        tStudent.setsBalance(stuUpdateBalance);
        if(tStudentMapper.updateByPrimaryKeySelective(tStudent)>0){
            return "更新成功";
        }else{
            return "更新失败";
        }
    }
}
